package pers.gxm.security.hello.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import pers.gxm.security.hello.common.R;
import pers.gxm.security.hello.mapper.UserMapper;
import pers.gxm.security.hello.pojo.User;
import pers.gxm.security.hello.service.SignUpService;

import java.time.LocalDateTime;
import java.util.Date;


@Service
public class SignUpServiceImpl implements SignUpService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override
    public R signUp(User user) {
//        TODO 1 判断用户名是否重复 这里可以加一段校验用户名的逻辑，具体可以和前端讨论
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(user.getUserName() != null,User::getUserName,user.getUserName());
        User userDb = userMapper.selectOne(userWrapper);
        if(userDb != null){
            return R.error(30001,"此用户名已注册，请重新输入！");
        }
//        TODO 2 注册 这里可以加一段校验密码复杂度的逻辑，具体可以和前端讨论
        String transparentPassword = user.getPassword();
        String passwordDb = passwordEncoder.encode(transparentPassword);
        user.setPassword(passwordDb);
        Date date = new Date();
        System.out.println("date = " + date);
        user.setCreateTime(date);
        user.setUpdateTime(date);
        userMapper.insert(user);
        return R.successWithMsg(30002,"注册成功!");
    }
}
